#include <iostream>
#include <string>
#include <vector>

// 定义一个结构来表示条件
struct Condition {
    std::string column;
    std::string op;
    std::string value;
};

void removeSpace(std::string &conditionsStr)
{
    std::string::iterator it = conditionsStr.begin();     //指向string类的迭代器。你可以理解为指针
	while (*it == ' ') {
        conditionsStr.erase(it);
        it = conditionsStr.begin();
    }
}

// 解析 SQL 查询语句中的条件
std::vector<Condition> parseConditions(const std::string& query) {
    std::vector<Condition> conditions;

    // 假设查询语句中条件部分以 WHERE 关键字开头
    size_t wherePos = query.find("WHERE");
    if (wherePos != std::string::npos) {
        // 获取 WHERE 关键字之后的条件部分
        std::string conditionsStr = query.substr(wherePos + 5);

        // 以 AND 连接多个条件，将其分割为多个子条件
        size_t andPos = 0;
        while ((andPos = conditionsStr.find("AND")) != std::string::npos) {
            std::string subConditionStr = conditionsStr.substr(0, andPos);

            
            conditionsStr = conditionsStr.substr(andPos + 4); // 跳过 AND 关键字

            removeSpace(subConditionStr);
            // 解析子条件中的列、操作符和值
            size_t spacePos = subConditionStr.find(" ");
            std::string column = subConditionStr.substr(0, spacePos);
            subConditionStr = subConditionStr.substr(spacePos + 1);

 
            removeSpace(subConditionStr);
            size_t opPos = subConditionStr.find_first_of("<>=");
            // 目前只考虑单字符运x算符
            std::string op = subConditionStr.substr(0, opPos + 1);
            subConditionStr = subConditionStr.substr(opPos + 1);

            // 移除值周围的空格
            subConditionStr.erase(0, subConditionStr.find_first_not_of(" "));
            // 解析值

            std::string value;
            if (subConditionStr.front() == '\'' || subConditionStr.front() == '"') {
                // 处理字符串值
                size_t endQuotePos = subConditionStr.find(subConditionStr.front(), 1);
                value = subConditionStr.substr(1, endQuotePos - 1);
            } else {
                // 处理非字符串值
                size_t spaceAfterValuePos = subConditionStr.find(" ");
                value = subConditionStr.substr(0, spaceAfterValuePos);
            }

            // 添加解析得到的条件到条件向量中
            conditions.push_back({column, op, value});
        }

        // 解析剩余的最后一个条件
        removeSpace(conditionsStr);
        size_t spacePos = conditionsStr.find(" ");
        std::string column = conditionsStr.substr(0, spacePos);
        conditionsStr = conditionsStr.substr(spacePos + 1);

        size_t opPos = conditionsStr.find_first_of("<>=");
        std::string op = conditionsStr.substr(0, opPos + 1);
        conditionsStr = conditionsStr.substr(opPos + 1);

        // 移除值周围的空格
        conditionsStr.erase(0, conditionsStr.find_first_not_of(" "));

        // 解析值
        std::string value;
        if (conditionsStr.front() == '\'' || conditionsStr.front() == '"') {
            // 处理字符串值
            size_t endQuotePos = conditionsStr.find(conditionsStr.front(), 1);
            value = conditionsStr.substr(1, endQuotePos - 1);
        } else {
            // 处理非字符串值
            size_t spaceAfterValuePos = conditionsStr.find(" ");
            value = conditionsStr.substr(0, spaceAfterValuePos);
        }

        // 添加解析得到的条件到条件向量中
        conditions.push_back({column, op, value});
    }

    return conditions;
}

int main() {
    // 示例 SQL 查询语句
    std::string query = "SELECT * FROM table WHERE age > 30 AND name = 'John' AND city = 'New York';";

    // 解析条件
    std::vector<Condition> conditions = parseConditions(query);

    // 打印解析得到的条件
    for (const auto& condition : conditions) {
        std::cout << "Column: " << condition.column << ", Operator: " << condition.op << ", Value: " << condition.value << std::endl;
    }

    return 0;
}
